package  com.syqc.sensor.oil

import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.custom.LeftRightTextView
import com.syqc.entity.OilDay
import com.syqc.monitor.R
import com.syqc.utils.Units

class OilInfoAdapter(private val teamName: String) :
    BaseQuickAdapter<OilDay, BaseViewHolder>(R.layout.item_sensor_day_oil) {
    var precision: String = "2"
    var oilUnit: String = ""
    private val unitTime by lazy { context.getString(R.string.unit_num) }

    init {
        addChildClickViewIds(R.id.ivDayInfo, R.id.lrvOilAdd, R.id.lrvOilLeak)
    }

    override fun convert(holder: BaseViewHolder, item: OilDay) {
        val position = holder.layoutPosition + 1
        val num = if (position < 10) "0$position" else "$position"
        holder.setText(R.id.tvItemNum, num)
        holder.run {
            item.run {
                val addTotal = getView<LeftRightTextView>(R.id.lrvOilAdd)
                if (OilModel.getUp()) {
                    addTotal.isVisible = true
                    addTotal.setRightText("$upTimes$unitTime  $upTotal$unit")
                    addTotal.nextVisible(upTimes > 0)
                    addTotal.isEnabled = upTimes > 0
                } else {
                    addTotal.isVisible = false
                }
                val leak = getView<LeftRightTextView>(R.id.lrvOilLeak)
                if (OilModel.getLeak()) {
                    leak.isVisible = true
                    leak.setRightText("$leakTimes$unitTime  $leakTotal$unit")
                    leak.nextVisible(leakTimes > 0)
                    leak.isEnabled = leakTimes > 0
                } else {
                    leak.isVisible = false
                }
                val lrvOilDate = getView<LeftRightTextView>(R.id.lrvOilDate)
                if (OilModel.getDate()) {
                    lrvOilDate.isVisible = true
                    lrvOilDate.setRightText(day)
                } else {
                    lrvOilDate.isVisible = false
                }

                val lrvOilTeam = getView<LeftRightTextView>(R.id.lrvOilTeam)
                if (OilModel.getTeam()) {
                    lrvOilTeam.isVisible = true
                    lrvOilTeam.setRightText(teamName)
                } else {
                    lrvOilTeam.isVisible = false
                }

                val lrvOilRun = getView<LeftRightTextView>(R.id.lrvOilRun)
                if (OilModel.getRun()) {
                    lrvOilRun.isVisible = true
                    lrvOilRun.setRightText("$runTotal$unit")
                } else {
                    lrvOilRun.isVisible = false
                }


                val lrvOilDown = getView<LeftRightTextView>(R.id.lrvOilDown)
                if (OilModel.getDown()) {
                    lrvOilDown.isVisible = true
                    lrvOilDown.setRightText("$downTotal $oilUnit")
                } else {
                    lrvOilDown.isVisible = false
                }

                val lrvOilSpeed = getView<LeftRightTextView>(R.id.lrvOilSpeed)
                if (OilModel.getSpeed()) {
                    lrvOilSpeed.isVisible = true
                    lrvOilSpeed.setRightText("$idlingTotal $oilUnit")
                } else {
                    lrvOilSpeed.isVisible = false
                }

                val lrvOilMile = getView<LeftRightTextView>(R.id.lrvOilMile)
                if (OilModel.getMile()) {
                    lrvOilMile.isVisible = true
                    val mileStr = if (mile > 1000) {
                        "$mileKm ${Units.km}"
                    } else {
                        "$mile ${Units.m}"
                    }
                    lrvOilMile.setRightText(mileStr)
                } else {
                    lrvOilMile.isVisible = false
                }

                val lrvOilTotal100 = getView<LeftRightTextView>(R.id.lrvOilTotal100)
                if (OilModel.getTotal100()) {
                    lrvOilTotal100.isVisible = true
                    lrvOilTotal100.setRightText("$totalBy100km$unit")
                } else {
                    lrvOilTotal100.isVisible = false
                }

                val lrvOilRun100 = getView<LeftRightTextView>(R.id.lrvOilRun100)
                if (OilModel.getRun100()) {
                    lrvOilRun100.isVisible = true
                    lrvOilRun100.setRightText("$runBy100km $oilUnit")
                } else {
                    lrvOilRun100.isVisible = false
                }

            }
        }
    }
}